package com.itheima.service.impl;

import cn.idev.excel.FastExcel;
import com.itheima.common.dto.OrderSettingDto;
import com.itheima.listener.OrderSettingDataListener;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @className OrderSettingServiceImpl
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {

    private final OrderSettingMapper orderSettingMapper;

    /**
     * 修改预约人数
     * @param orderSettingDto 预约条件
     * @return void
     **/
    public void editNumberByOrderDate(OrderSettingDto orderSettingDto) {
        OrderSetting orderSetting = new OrderSetting();
        BeanUtils.copyProperties(orderSettingDto, orderSetting);
        orderSettingMapper.updateNumberByOrderDate(orderSetting);
    }

    /**
     * 根据月份查询预约人数
     * @param month 月份
     * @return List<OrderSetting>
     **/
    public List<OrderSetting> getOrderSettingByMonth(String month) {
        return orderSettingMapper.selectOrderSettingByMonth(month);
    }

    /**
     * 上传设置预约人数的文件
     * @param excelFile excel文件
     * @return void
     **/
    public void uploadFile(MultipartFile excelFile) {
        try (InputStream inputStream = excelFile.getInputStream()) {
            OrderSettingDataListener listener = new OrderSettingDataListener(orderSettingMapper);
            FastExcel.read(inputStream, OrderSetting.class, listener)
                    // 指定读取第一个sheet，默认第一个
                    .sheet()
                    // 指定第二行读取，第一行是标题
                    .headRowNumber(1)
                    .doRead();
            log.info("Excel文件读取成功");
        } catch (IOException e) {
            log.error("Excel文件读取失败：{}", e.getMessage());
            throw new RuntimeException("文件处理失败", e);
        }
    }
}
